<?php
	/**
	 * Created by PhpStorm.
	 * User: mybook-lhp
	 * Date: 18/6/9
	 * Time: 上午10:31
	 */

	namespace app\store\api;


	use app\common\controller\API;
	use app\store\model\StoreaddressModel;

	class Regoin extends API
	{
		/**
		 * 地址列表
		 * api POST /store/Storeaddress/lists
		 *
		 * @throws \think\db\exception\DataNotFoundException
		 * @throws \think\db\exception\ModelNotFoundException
		 * @throws \think\exception\DbException
		 */
		public function lists()
		{
			$addresses = StoreaddressModel::where(['member_id' => $this->member_id])->select();
			foreach ($addresses as &$item)
			{
				if (isset($item['address_area']))
				{
					$item['address_area'] = json_decode($item['address_area']);
				}
			}
			unset($item);
			$this->result($addresses);
		}

		/**
		 * 获取默认地址
		 * api POST /store/Storeaddress/defautl
		 * @throws \think\db\exception\DataNotFoundException
		 * @throws \think\db\exception\ModelNotFoundException
		 * @throws \think\exception\DbException
		 */
		public function defautl()
		{
			$def = StoreaddressModel::where(['member_id' => $this->member_id, 'is_default' => 1])->find();

			if (!$def)
			{
				$def = StoreaddressModel::where(['member_id' => $this->member_id])->find();
				if ($def)
				{
					$def['address_area'] = json_decode($def['address_area']);
					$this->result($def);
				}
				$this->result([]);

			} else
			{
				$def['address_area'] = json_decode($def['address_area']);
				$this->result($def);
			}

		}

		/**
		 * 添加地址
		 * api POST /store/Storeaddress/add
		 * @param array $address_area 地址数组按顺序(省，市，区，县 )
		 * @param string $name 收货人
		 * @param string $mobile 手机号
		 * @param string $address_detail 详细
		 * @param integer $is_default 是否设置为默认地址(可选)
		 * @return string $code  200-成功500-失败
		 */
		public function add()
		{
			$param = $this->request->param();

			$data['member_id'] = $this->member_id;

			if (isset($param['name']) && !empty($param['name']))
			{
				$data['name'] = $param['name'];
			} else
			{
				$this->error('收货人不得为空!');
			}
			if (isset($param['mobile']) && !empty($param['mobile']))
			{
				$data['mobile'] = $param['mobile'];
			} else
			{
				$this->error('手机号不能为空!');
			}
			if (isset($param['address_area']) && !empty($param['address_area']))
			{
				$data['address_area'] = json_encode($param['address_area']);
			} else
			{
				$this->error('未添加行政区!');
			}

			if (isset($param['address_detail']) && $param['address_detail'] != '')
			{
				$data['address_detail'] = $param['address_detail'];
			} else
			{
				$this->error('详细地址不能为空!');
			}


			if (isset($param['is_default']))
			{
				StoreaddressModel::update(['is_default' => 0], ['member_id' => $this->member_id]);

			}
			$data['is_default'] = $param['is_default'];
			if (StoreaddressModel::create($data))
			{
				$this->success('添加成功！');
			} else
			{
				$this->error('添加失败');
			}
		}

		/**
		 * 更新地址
		 * api POST /store/Storeaddress/update
		 * @param integer $id 地址ID
		 * @param array $address_area 地址数组按顺序(省，市，区，县 )
		 * @param string $name 收货人
		 * @param string $mobile 手机号
		 * @param string $address_detail 详细
		 * @param integer $is_default 是否设置为默认地址(可选)
		 * @return string $code  200-成功500-失败
		 */
		public function update()
		{
			$param = $this->request->param();

			if (isset($param['id']) && !empty($param['id']))
			{
				$data['id'] = $param['id'];
			} else
			{
				$this->error('缺少ID，参数错误！');
			}
			if (isset($param['name']) && !empty($param['name']))
			{
				$data['name'] = $param['name'];
			}
			if (isset($param['mobile']) && !empty($param['mobile']))
			{
				$data['mobile'] = $param['mobile'];
			}
			if (isset($param['address_area']) && !empty($param['address_area']))
			{
				$data['address_area'] = json_encode($param['address_area']);
			}

			if (isset($param['address_detail']) && $param['address_detail'] != '')
			{
				$data['address_detail'] = $param['address_detail'];
			}


			if (isset($param['is_default']) && $param['is_default'] == 1)
			{

				StoreaddressModel::update(['is_default' => 0], ['member_id' => $this->member_id]);

			}
			$data['is_default'] = $param['is_default'];
			$re = StoreaddressModel::update($data, ['member_id' => $this->member_id, 'id' => $param['id']]);
			if ($re)
			{
				$this->success('更新成功！');
			} else
			{
				$this->error('更新失败');
			}
		}

		/**
		 * 删除地址
		 * api POST /store/Storeaddress/del
		 * @param  integer $ids 商品ID，逗号分隔-例如：1，2，3，4
		 * @return string $code  200-成功500-失败
		 * @return string $msg  错误说明
		 * @return string $data[]
		 */
		public function del()
		{
			$ids = $this->request->param('ids', false);
			if (isset($ids))
			{
				if (StoreaddressModel::whereIn('id', $ids)->delete())
				{
					$this->success('删除成功！');
				} else
				{
					$this->error('删除失败');
				}

			} else
			{
				$this->error('参数错误！');
			}
		}

	}